﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Help;
using Model;

namespace BLLA
{
    public partial class Order_DayReport_Mer_T : BaseBLL<Model.Order_DayReport_Mer_T>,IBLL.IOrder_DayReport_Mer_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();
       

        /// <summary>
        /// 生成商户总日结表
        /// </summary>
        /// <param name="MerID"></param>
        /// <param name="YZM"></param>
        /// <param name="RETID"></param>
        /// <returns></returns>
        public void DayReport_Mer_T()
        {

            string time = DateTime.Now.ToString("yyyyMMdd");//当天时间
            List<Model.Order_Mer_T> mo = new List<Model.Order_Mer_T>();
            List<Model.Order_Form_T> AllForm = new List<Model.Order_Form_T>();
            List<Model.Order_DayReport_Mer_T> DayReport = new List<Model.Order_DayReport_Mer_T>();//商户总结

            mo = base.DBSession.IOrder_Mer_TDAL.GetListBy(u => u.MerState == 1);//获取所有商户
            foreach (var it in mo)//遍历商户
            {
                AllForm = base.DBSession.IOrder_Form_TDAL.GetListBy("SELECT * FROM  Order_Form_T where FormTime>'" + time + "000000" + "' and FormTime<'" + time + "235959" + "' and FormMerID='" + it.MerID + "'and FormState='" + 1 + "'");//获取商户一天有效的订单

                if (AllForm.Count != 0)
                {
                    Model.Order_DayReport_Mer_T DayRe = new Model.Order_DayReport_Mer_T();
                     int MerDiscount = 0;
                    foreach (var item in AllForm)//遍历订单
                    {
                        string[] str = item.FormDishesMon.Split(',');//集合变字符串数组
                        for (int i = 0; i < str.Length; i++)
                        {
                            DayRe.MerOriginal += int.Parse(str[i]);//菜品原价
                        }
                        DayRe.MerReceivables += item.FormAllMon;//应收
                        MerDiscount += int.Parse(item.FormPreMon.ToString());// 优惠
                        DayRe.MerActual += item.FormPayMon;//实收
                    }
                    DayRe.MerDiscount = MerDiscount;
                    DayRe.MerID = it.MerID;//商户id
                    DayRe.DayReportDate = time;//日期8位
                    DayReport.Add(DayRe);
                }
                base.DBSession.IOrder_DayReport_Mer_TDAL.Add(DayReport);
            }
        }

        /// <summary>
        /// 生成商户分类日结表
        /// </summary>
        public void DayReport_DishesType_T()
        {
            //string time = DateTime.Now.ToString("yyyyMMdd");//当天时间
            List<Model.Order_Mer_T> mo = new List<Model.Order_Mer_T>();//商户
            List<Model.Order_Form_T> AllForm = new List<Model.Order_Form_T>();//订单
            Model.Order_DayReport_Mer_T DayReport = new Model.Order_DayReport_Mer_T();//商户总日结
            List<Model.Order_DishesType_T> DishesT = new List<Model.Order_DishesType_T>();//菜品分类
            List<Model.Order_DayReport_DishesType_T> DishesType = new List<Model.Order_DayReport_DishesType_T>();//商户分类日结
            Model.Order_Dishes_T Dishes=new Model.Order_Dishes_T();//菜品信息
            mo = base.DBSession.IOrder_Mer_TDAL.GetListBy(u => u.MerState == 1);//获取所有商户
            if (mo.Count!=0)
            {
                foreach (var item in mo)
                {
                    DishesT = base.DBSession.IOrder_DishesType_TDAL.GetListBy(u => u.MerID==item.MerID);//商户下所有菜品分类来源
                    Model.Order_DayReport_DishesType_T dic = new Model.Order_DayReport_DishesType_T();
                    DayReport = base.DBSession.IOrder_DayReport_Mer_TDAL.GetListBy(u => u.MerID == item.MerID).First();//一个商户一天只能有一条商户总日结记录
                    dic.MerDayReportID = DayReport.MerDayReportID;//总日结id
                    AllForm = base.DBSession.IOrder_Form_TDAL.GetListBy(u => u.FormTime == DayReport.DayReportDate || u.FormState == 1);//在商户总日结的当天时间的所有订单
                    if (AllForm.Count != 0)
                    {
                        foreach (var st in AllForm)//轮询订单
                        {
                            string[] form = st.FormDishesID.Split(',');//菜品id集合
                            string[] DishesMon = st.FormDishesMon.Split(',');//原价集合
                           //每个菜品id都放入相应的类中
                            for (int i = 0; i < form.Length; i++)//轮询菜品id集合
                            {
                                Dishes=base.DBSession.IOrder_Dishes_TDAL.GetListBy(u =>u.DishesID==form[i]).First();//一个菜品id一条菜品信息查菜品实际价格菜品分类id
                                foreach (var mer in DishesT)//轮询商户菜品分类,多少类，循环多少个DishesType
                                {
                                    if (mer.DishesTypeID== Dishes.DishesTypeID)//判断菜品分类id是否相等
                                    {
                                        dic.DishesTypeID = mer.DishesTypeID;//菜品分类中菜品分类id等日结表分类id
                                        dic.TypeOriginal+=int.Parse(DishesMon[i]);//同类原价总和
                                        dic.TypeActual +=int.Parse( Dishes.DishesPay.ToString());//实收总和
                                    }
                                    
                                }
                                
                            }

                        }
                        dic.TypeDayReportID = DayReport.MerDayReportID;//总日结id
                        base.DBSession.IOrder_DayReport_DishesType_TDAL.Add(dic);
                    }
                }  
                  
            }
        }

        ///<summary>
        ///商户分类日结表
        ///</summary>
        ///<param name="MerID"></param>
        ///<param name="time1">开始时间</param>
        ///<param name="time2">结束时间</param>
        ///<param name="YZM"></param>
        ///<param name="RETID"></param>
        ///<returns></returns>
        //public string GetBusiness_table(int MerID, string time1, string time2, string YZM, int RETID)
        //{
        //    try
        //    {
        //        #region 匹配验证码
        //        if (YZM != Help.ALL_DataV.YZM)
        //        {
        //            result.BS = "-88";
        //            result.Msg = "验证码错误！";
        //            return os.StringOfObject(result, RETID);
        //        }
        //        #endregion

        //        List<Model.Order_DishesType_T> DishesType = new List<Model.Order_DishesType_T>();//菜品分类
        //        List<Model.Order_Form_T> Allform = new List<Model.Order_Form_T>();//订单
        //        //List<Model.BusinessRepsort> Business = new List<Model.BusinessRepsort>();//营业统计
        //        List<Model.Order_Dishes_T> Dishes = new List<Model.Order_Dishes_T>();//商品信息（查分类id）

        //        int TotalMon = 0;//总计
        //        try
        //        {
        //            //根据商户id查询商户下所有DishesTypeID和对应菜品分类
        //            DishesType = base.DBSession.IOrder_DishesType_TDAL.GetListBy("SELECT * FROM Order_DishesType_T WHERE  MerID=" + MerID);
        //            //根据商户id获取商户下时间段的订单
        //            Allform = base.DBSession.IOrder_Form_TDAL.GetListBy("SELECT * FROM  Order_Form_T where FormTime>'" + time1 + "000000" + "' and FormTime<'" + time2 + "235959" + "' and FormMerID='" + MerID + "'");

        //            foreach (var item in Allform)
        //            {
        //                string[] DisID = item.FormDishesID.Split(',');//菜品id集合转换为字符串数组
        //                string[] DishesMon = item.FormDishesMon.Split(',');//菜品原价集合
        //                foreach (var it in DishesType)
        //                {
        //                    for (int i = 0; i < DisID.Length; i++)
        //                    {
        //                        Dishes = base.DBSession.IOrder_Dishes_TDAL.GetListBy(u => u.DishesID == DisID[i]);//根据菜品id获取菜品信息
        //                        if (it.DishesTypeID == "?")//判断菜品id属于哪个菜品
        //                        {
        //                            //获取菜品原价
        //                            //实际价格num+
        //                        }
        //                    }
        //                }

        //            }
        //        }
        //        catch (Exception)
        //        {
        //            result.BS = "-1";
        //            result.Msg = "查询不到用户！";
        //            return os.StringOfObject(result, RETID);
        //        }
        //        return null;
        //    }
        //    catch (Exception ex)
        //    {
        //        WriteLog.WriteW("获取营业统计表数据失败：" + ex.Message);
        //        result.BS = "-99";
        //        result.Msg = "系统错误！";
        //        return os.StringOfObject(result, RETID);
        //    }
        //}

    //    /// <summary>商户菜品日结表
    //    ///根据商户id及该商户在时间段内查询营业流水
    //    /// </summary>
    //    /// <param name="MerID"></param>
    //    /// <param name="date">日结时间</param>
    //    ///  <param name="YZM"></param>
    //    /// <param name="RETID"></param>
    //    /// <returns></returns>
    //    public string GetBusinessFlow_table(int MerID, string date, string YZM, int RETID)
    //    {
    //        try
    //        {
    //            #region 匹配验证码
    //            if (YZM != Help.ALL_DataV.YZM)
    //            {
    //                result.BS = "-88";
    //                result.Msg = "验证码错误！";
    //                return os.StringOfObject(result, RETID);
    //            }
    //            #endregion
    //            //获取该商户下所有菜品（id，菜品）：商品信息表：Order_Dishes_T
    //            List<Model.Order_Form_T> AllForm = base.DBSession.IOrder_Form_TDAL.GetListBy(u => u.FormTime == date || u.FormMerID == MerID.ToString() || u.FormState == 1); //获取该商户下一天内所有订单
    //            try
    //            {


    //            }
    //            catch (Exception)
    //            {
    //                result.BS = "-1";
    //                result.Msg = "查询不到用户！";
    //                return os.StringOfObject(result, RETID);
    //            }
    //            return null;
    //        }
    //        catch (Exception ex)
    //        {
    //            WriteLog.WriteW("获取营业流水表数据失败：" + ex.Message);
    //            result.BS = "-99";
    //            result.Msg = "系统错误！";
    //            return os.StringOfObject(result, RETID);
    //        }
    //    }
    }
   
}
